/**
 * @description: 数组重分类
 * @param {array} arr 重分类的数组
 * @param {string} str 重分类字段名
 * @return {array}
 */
function regroup(arr, str) {
  var map = {},
    dest = [];
  arr.map((item) => {
    if (!map[item[str]]) {
      dest.push({
        id: item[str],
        data: [item],
      });
      map[item[str]] = item;
    } else {
      dest.filter((itemD) => itemD.id == item[str])[0].data.push(item);
    }
  });
  return dest;
}
function reEsgroup(arr, str) {
  var map = {},
    dest = [];
  arr.map((item) => {
    if (!map[item._source[str]]) {
      dest.push({
        id: item._source[str],
        data: [item],
      });
      map[item._source[str]] = item;
    } else {
      dest.filter((itemD) => itemD.id == item._source[str])[0].data.push(item);
    }
  });
  return dest;
}
//太抽象的不会写le,处理数据用的
function array2Tree(array) {
  let tree = regroup(array, "大分类");
  tree.map((item) => {
    item.poicode = item.data[0].poicode.toString().slice(0, 3);
  });
  for (let j = 0; j < tree.length; j++) {
    var first = regroup(tree[j].data, "中分类");
    delete tree[j].data;
    first.map((item) => {
      item.poicode = item.data[0].poicode.toString().slice(0, 4);
    });
    tree[j].children = first;
    for (let i = 0; i < first.length; i++) {
      var second = regroup(first[i].data, "小分类");
      delete first[i].data;
      tree[j].children[i].children = [];
      second.map((item) => {
        item.data.map((item) => {
          delete item.大分类;
          delete item.中分类;
          item.FLNAME = item.小分类;
          item.poicode = item.poicode.toString();
          delete item.小分类;
          tree[j].children[i].children.push(item);
        });
      });
    }
  }
  return tree;
}
export { regroup, reEsgroup, array2Tree };
